package com.qk.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.dto.ClueDto;
import com.qk.dto.MarkFalseClueDto;
import com.qk.entity.Business;
import com.qk.entity.Clue;
import com.qk.entity.ClueTrackRecord;
import com.qk.mapper.BusinessMapper;
import com.qk.mapper.ClueTrackRecordMapper;
import com.qk.service.ClueService;
import com.qk.utils.CurrentUserHoler;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class ClueServiceImpl extends ServiceImpl<com.qk.mapper.ClueMapper, Clue> implements ClueService {
    @Autowired
    private com.qk.mapper.ClueMapper clueMapper;
    @Autowired
    private ClueTrackRecordMapper clueTrackRecordMapper;
    @Autowired
    private BusinessMapper businessMapper;

    @Override
    public PageResult<Clue> listClues(ClueDto clueDto) {
        Page<Clue> page = clueMapper.listClues(new Page<Clue>(clueDto.getPage(), clueDto.getPageSize()), clueDto);
        return new PageResult<>(page.getTotal(), page.getRecords());
    }

    @Override
    public Clue getClueById(Integer id) {
        return clueMapper.getClueById(id);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void trackClue(Clue clue)  {
        //1.根据id更新线索的基本信息
        clue.setStatus(3);//跟进中
        clue.setUpdateTime(LocalDateTime.now());
        clueMapper.updateById(clue);
        //2.添加线索的跟进记录
        ClueTrackRecord clueTrackRecoed = new ClueTrackRecord();
        clueTrackRecoed.setClueId(clue.getId());//线索id
        clueTrackRecoed.setUserId(CurrentUserHoler.getCurrentUser());//线索人id,当前登录用户id
        clueTrackRecoed.setSubject(clue.getSubject());
        clueTrackRecoed.setRecord(clue.getRecord());
        clueTrackRecoed.setLevel(clue.getLevel());
        clueTrackRecoed.setNextTime(clue.getNextTime());
        clueTrackRecoed.setType(1);
        clueTrackRecoed.setCreateTime(LocalDateTime.now());
        clueTrackRecordMapper.insert(clueTrackRecoed);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void markFalseClue(Integer id, MarkFalseClueDto markFalseClueDto) {
        Clue clue = new Clue();
        clue.setId(id);
        clue.setStatus(4);
        clue.setUpdateTime(LocalDateTime.now());
        clueMapper.updateById(clue);

        //添加线索的跟进记录
        ClueTrackRecord clueTrackRecoed = new ClueTrackRecord();
        clueTrackRecoed.setClueId(id);
        clueTrackRecoed.setUserId(CurrentUserHoler.getCurrentUser());
        clueTrackRecoed.setFalseReason(markFalseClueDto.getReason());
        clueTrackRecoed.setRecord(markFalseClueDto.getRemark());
        clueTrackRecoed.setType(0);
        clueTrackRecoed.setCreateTime(LocalDateTime.now());
        clueTrackRecordMapper.insert(clueTrackRecoed);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void toBusiness(Integer id) {
        //1.更新线索状态
        Clue clue = clueMapper.getClueById(id);
        clue.setStatus(5);
        clue.setUpdateTime(LocalDateTime.now());
        clueMapper.updateById(clue);
        //2.保存商机信息(根据线索,组装商机信息)
        Business business = new Business();
        BeanUtils.copyProperties(clue,business);
        business.setId(null);//设置商机Id为空,表示新增Id,主键自增
        business.setUserId(null);//设置商机归属人为空.
        business.setNextTime( null);//设置下次联系时间为空
        business.setClueId(clue.getId());
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());
        business.setStatus(1);

        businessMapper.insert(business);

    }

    @Override
    public PageResult<Clue> getClue(ClueDto clueDto) {
        Page<Clue> page = clueMapper.getClue(new Page<Clue>(clueDto.getPage(), clueDto.getPageSize()), clueDto);
        return new PageResult<>(page.getTotal(), page.getRecords());
    }


}
